package com.carcontrol.rotate;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;


/**
 * 自定义接收器
 * 
 * 如果不定义这个 Receiver，则：
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class MyReceiver extends BroadcastReceiver {
	private static final String TAG = "MyReceiver";

	@Override
	public void onReceive(Context context, Intent intent) {
		try {
//			Bundle bundle = intent.getExtras();
			String action = intent.getAction().toString();
			Log.e("MyReceiver===", "[MyReceiver] onReceive - " + action);


			if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {

				Log.e("MyReceiver===1", "COMPLETED===");//

				Intent intentMainActivity = new Intent(context, MainActivity.class);
//				intentMainActivity.setAction("android.intent.action.MAIN");
//				intentMainActivity.addCategory("android.intent.category.LAUNCHER");
//				intentMainActivity.addCategory("android.intent.category.DEFAULT");
				intentMainActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

				Log.e("MyReceiver===2", "COMPLETED===");

				context.startActivity(intentMainActivity);
//				Toast.makeText(context, "开机完毕~", Toast.LENGTH_LONG).show();

				Log.e("MyReceiver===3", "COMPLETED===");
			}else {
				Log.e("MyReceiver===3", "[MyReceiver] Unhandled intent - " + intent.getAction());
			}
		} catch (Exception e){
			Log.e("MyReceiver===e", "===" + e);
		}

	}

	
	//send msg to MainActivity
	private void processCustomMessage(Context context, Bundle bundle) {
//		if (MainActivity.isForeground) {
//			String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
//			String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
//			Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
//			msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);
//			if (!ExampleUtil.isEmpty(extras)) {
//				try {
//					JSONObject extraJson = new JSONObject(extras);
//					if (extraJson.length() > 0) {
//						msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
//					}
//				} catch (JSONException e) {
//
//				}
//
//			}
//			LocalBroadcastManager.getInstance(context).sendBroadcast(msgIntent);
//		}
	}
}
